

function parentGet( id )
{
    return window.opener.document.getElementById( id );
}



function linkTo(assessmentIdentity, columnValues)
{
    var tbody = parentGet( 'tbody-' + componentIdentity );
    if( tbody )
        return linkToTable( tbody, assessmentIdentity, columnValues );
      
    // No table body specified, must be a select widget?
    var select = parentGet( componentIdentity );
    return linkToSelect( select, assessmentIdentity, columnValues );

}

function linkToSelect( select, assessmentIdentity, columnValues )
{
    
    // We must use DOM rather than new Option() here as IE doesn't like cross-page option insertion
    // See - http://www.mredkj.com/tutorials/tutorial008.html
    var option = window.opener.document.createElement("option");
    option.value = assessmentIdentity;
    option.innerHTML = columnValues[0];
    option.selected = true;
    
    select.appendChild( option );
     
    window.close();
    
}

function linkToTable( tbody, assessmentIdentity, columnValues )
{
    // If the component isn't multivalued, remove existing references
    
    //alert(tbody.innerHTML);
    
    if( tbody.className != 'multivalued' )
    {
        // Remove all existing children
        var childNode;
        while( childNode = tbody.firstChild )
        {
            tbody.removeChild( childNode );
        }
        //tbody.innerHTML = "<tr></tr>";
    }

    // Create the table structure
    var tr = window.opener.document.createElement('TR');
    tbody.appendChild( tr );

    for( x=0; x<columnValues.length; x++ )
    {
    
        var td = window.opener.document.createElement('td');
            
        var span = window.opener.document.createElement('span');
        span.innerHTML = columnValues[x];
    
        // Finish the table structure
    
        td.appendChild( span );
        tr.appendChild( td );
    }

    
    // Create the actions cell to hold the 'remove' link
    var actionTD = window.opener.document.createElement('td');
    actionTD.innerHTML = '<' + 'a href="#" onClick="removeThisRow(this); return false">Remove<' + '/a>';
    tr.appendChild( actionTD );

    var field = window.opener.document.createElement('input');
    field.name  = componentIdentity + ":list";
    field.value = assessmentIdentity;
    field.type  = "hidden";

    actionTD.appendChild( field );    

    window.close();
}